#coding:utf8
"""
获取网易历史行情数据以及资金流向数据
"""
import os,re,json
import requests
import pandas as pd
import efinance as ef
from datetime import datetime


def get_history_hangq(code,start,end=None):
    """

    :return:
    """
    #code = '689009'
    if str(code[0]) == "6":
        ucode = '0' + code
    else:
        ucode = "1" + code

    url = f"""http://quotes.money.163.com/service/chddata.html?code={ucode}&start={start}&end={end}&fields=TCLOSE;HIGH;LOW;TOPEN;LCLOSE;CHG;PCHG;TURNOVER;VOTURNOVER;VATURNOVER;TCAP;MCAP"""
    res = requests.get(url=url)
    res.encoding = "gbk"
    t1 = res.text
    t2 = t1.split("\r\n")
    t3 = t2[0].split(',')
    t3[2] = '股票名称'
    data = [line.split(',') for line in t2[1:]]

    try:
        df1 = pd.DataFrame(data=data, columns=t3)
    except Exception as e:
        print(f"{e.__str__()}")
        return False

    return True, df1


def get_history_zjlx(code):
    """
    数据源，efinance，东方财富
    :param code:
    :return:
    """
    df1 = ef.stock.get_history_bill(code)
    if len(df1) < 10:
        print(f"{code}: 历史资金流向数据小于10天")
        return False

    return True, df1


def get_stock_data(code, start, end=None):
    """

    :param code:
    :return:
    """
    #获取行情数据,网易数据源
    result_1 = get_history_zjlx(code)
    if not result_1:
        return None

    df1 = result_1[1]
    result_2 = get_history_hangq(code=code, start=start, end=end)
    if not result_2:
        return None

    df2 = result_2[1]
    df3 = pd.merge(df2, df1, on=['股票名称', '日期'])

    t1 = datetime.now()
    t2 = t1.strftime('%Y-%m-%d')
    save_dir = os.path.join(r"../data/history", t2)
    if not os.path.exists(save_dir):
        os.makedirs(save_dir)

    fileName = str(code) + ".csv"
    filePath = os.path.join(save_dir, fileName)
    f = open(filePath, "w", encoding="utf8")
    df3.to_csv(f, sep=',',lineterminator="\n")
    f.close()


def main():
    start = "20220826"
    end = "20221208"
    df1 = ef.stock.get_realtime_quotes()
    codes_list = df1['股票代码'].tolist()

    cnt = 0
    for code in codes_list:
        if re.findall(r"^2|^4|^9|^8", code):
            continue
        cnt += 1
        print(f"processing: {code}, {cnt}/{len(codes_list)}")
        get_stock_data(code, start, end)


if __name__=="__main__":
    start = '20221125'
    end = '20221213'
    main()

